package com.xiaoxing.paimai.mvp.ui.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jess.arms.utils.ArmsUtils;
import com.xiaoxing.paimai.R;
import com.xiaoxing.paimai.mvp.ui.entity.ChongDianGui;
import com.xiaoxing.paimai.mvp.ui.fragment.HomeFragment;
import com.xw.repo.XEditText;

import java.util.HashMap;
import java.util.List;


public class ChongDianGuiAdapter extends BaseQuickAdapter<ChongDianGui, BaseViewHolder> {

    private Context mContext;

    public ChongDianGuiAdapter(Context context, @Nullable List<ChongDianGui> data) {
        super(R.layout.item_chong_dian_gui, data);
        this.mContext = context;
    }

    @Override
    protected void convert(BaseViewHolder helper, ChongDianGui item) {

        Glide.with(mContext).load(item.getImgPath()).into((ImageView) helper.getView(R.id.img_chan_pin));
        helper.setText(R.id.tv_name, item.getXingHao());
        helper.setText(R.id.tv_keyong_shu_liang, item.getKeYongShuLiang());


        XEditText xEditText = helper.getView(R.id.xet_yu_yue_ge_shu);
        xEditText.setText("");
        xEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

                String editValue = xEditText.getText().toString();
                if (!TextUtils.isEmpty(editValue) && Integer.parseInt(editValue) > 0) {

                    if (Integer.parseInt(editValue) > Integer.parseInt(item.getKeYongShuLiang())) {
                        ArmsUtils.snackbarText("预订数量不能大于可预约数量");
                        xEditText.setText(item.getKeYongShuLiang());
                    } else {
                        HashMap<String, String> map = new HashMap<>();
                        map.put(item.getXingHao(), editValue);
                        HomeFragment.instance.mYuYueHashMap.put(item.getId(), map);
                    }

                } else {
                    HomeFragment.instance.mYuYueHashMap.remove(item.getId());
                }
            }
        });
    }


}
